

#import "LiWuVc.h"

#import "liwuCell.h"

#import "liwu.h"


#import "coins_B1.h"
#import "GlobalShareData.h"
#import "DOUAudioEventLoop.h"



@interface LiWuVc ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>{
    
    NSMutableArray*liwuData;

}
@property(nonatomic,strong)UICollectionView*ShowCollectionView;

@end

@implementation LiWuVc

- (void)viewDidLoad {
        
    [super viewDidLoad];
    
    [self Get_giftshop];
    
    [self creatUI];
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"back" highIcon:@"back" target:self action:@selector(back_back)];
    
}

-(void)creatUI{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    _ShowCollectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-100)collectionViewLayout:flowLayout];
    _ShowCollectionView.delegate=self;
    _ShowCollectionView.dataSource=self;
    _ShowCollectionView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:_ShowCollectionView];
    
}

#pragma mark - 所有礼物 -
-(void)Get_giftshop
{
    
    [MBProgressHUD showWithStatus:@"加载中..." toView:self.view];
    
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    
    NSString*infoStr11=[NSString stringWithFormat:@"/api/gifts?page=0&size=20&sort=createdDate,DESC"];
    
    NSString *intactURL = [MyBaseUrl stringByAppendingString:infoStr11];
    
    NSString * token = [FileStorage NSUserDefault_getObjectForKey:access_token];
    
    manager.requestSerializer =[AFHTTPRequestSerializer serializer];
    
    [manager.requestSerializer setValue:@"application/json"
                     forHTTPHeaderField:@"Accept"];
    [manager.requestSerializer setValue:[NSString stringWithFormat: @"Bearer %@",token]
                     forHTTPHeaderField:@"Authorization"];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager GET:intactURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary*dic=[NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        
        NSLog(@"成功礼物GET --> %@", dic);
        
        liwuData=[NSMutableArray array];
        
        NSLog(@"resultCode:%@",dic[@"resultCode"]);
        
        NSArray*content =dic[@"body"][@"content"];
        
        if (content.count>0) {
            
        for (NSDictionary *guibin_Datas in content) {
            liwu*aa=[[liwu alloc]init];
            
            aa.aid=guibin_Datas[@"id"];
            aa.name=guibin_Datas[@"name"];
            aa.pictureUrl=guibin_Datas[@"pictureUrl"];
            
            aa.price=guibin_Datas[@"price"];
            aa.popularity=guibin_Datas[@"popularity"];
            aa.heat=guibin_Datas[@"heat"];
            aa.heatValidityTime=guibin_Datas[@"heatValidityTime"];
            aa.discount=guibin_Datas[@"discount"];
            
            aa.descriptionD=guibin_Datas[@"description"];
            aa.isLuxury=guibin_Datas[@"isLuxury"];
            aa.createdBy=guibin_Datas[@"createdBy"];
            aa.createdDate=guibin_Datas[@"createdDate"];
            aa.lastModifiedBy=guibin_Datas[@"lastModifiedBy"];
            
            aa.lastModifiedDate=guibin_Datas[@"lastModifiedDate"];

            [aa fixedString];
            [liwuData addObject:aa];
            
        }
        
        [_ShowCollectionView reloadData];
            
        [User_information conversion].GIFT_SHOP_Data = [liwuData copy];
            
        }else{
            
            liwuData=nil;
            [_ShowCollectionView reloadData];

        }
        [MBProgressHUD hideHUDForView:self.view animated:YES];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"%@",error);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [MBProgressHUD showErrorWithStatus:@"数据获取失败" toView:self.view];
    }];
}

- (void)back_back
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return liwuData.count;
}

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    [_ShowCollectionView registerClass:[liwuCell class] forCellWithReuseIdentifier:@"liwuCell"];
    
    liwuCell*cell=(liwuCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"liwuCell" forIndexPath:indexPath];
    
    cell.backgroundColor=[UIColor whiteColor];
    liwu*lw=[[liwu alloc]init];
    lw=liwuData[indexPath.row];
    [cell.dt sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",lw.pictureUrl]] placeholderImage:nil];
    cell.my_name.text=[NSString stringWithFormat:@"%@",lw.name];
    cell.mylabel.text=[NSString stringWithFormat:@"%@金币",lw.price];
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"购买-%ld-",indexPath.row);
    
    [self.navigationController pushViewController:[coins_B1 new] animated:YES];
    
}

//定义每一个cell大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(WIDTH/3-1, 150);
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 1;
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 1;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
